package com.zhanqiao.controller;

import com.alibaba.fastjson.JSON;
import com.mchange.v2.c3p0.ComboPooledDataSource;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayHandler;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.SQLException;
import java.util.*;

@WebServlet("/check.do")
public class CheckController extends HttpServlet{

    private String uname1;
    private String password1;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //检验数据库里面值用来登录界面
        String uname = req.getParameter("username");
        String password = req.getParameter("password");
        resp.setContentType("application/json;charset=utf8");
        //连接数据库
        DataSource dataSource = new ComboPooledDataSource();
        QueryRunner queryRunner = new QueryRunner(dataSource);
        //查询出用户名和密码即可
        String sql = "select count(*) from admin where aname = ? and apwd =?";
        Object[] parm = {uname,password};
        try {
            Object[] query = queryRunner.query(sql, new ArrayHandler(), parm);
            Map message = new HashMap();
            if(Integer.parseInt(query[0]+"")>0){
                message.put("msg","登陆成功");
            }else{
                message.put("msg","登录失败");
            }
            String res = JSON.toJSONString(message);
           // resp.getWriter().write(res);
            req.getRequestDispatcher("/main.html").forward(req, resp);
        } catch (SQLException e) {
            e.printStackTrace();
        }



    }
}
